package rsj.service.customer;

import com.alibaba.fastjson.JSON;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rsj.api.customer.entity.Customer;
import rsj.api.customer.entity.CustomerQueryParam;
import common.entity.PageInfo;
import rsj.sql.customer.CustomerDao;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 客户
 *
 * @author yuanqinglong
 * @since 2021/7/14 11:23
 */
@Service
public class CustomerServiceImpl implements ICustomerService {


    @Autowired
    private CustomerDao customerDao;

    /**
     * 客户列表
     *
     * @param customerQueryParam 客户查询参数
     * @return {@link List<Customer>}
     */
    @Override
    public List<Customer> customerList(CustomerQueryParam customerQueryParam) {
        PageInfo pageInfo = customerQueryParam.getPageInfo();
        TableNode tableNode = customerDao.customerList(customerQueryParam.getCustSname(), customerQueryParam.getCustId(), pageInfo.getPageSize(), pageInfo.getPageOffSet());
        return JSON.parseArray(tableNode.toJson(), Customer.class);
    }


    /**
     * 客户数
     *
     * @param customerQueryParam 客户查询参数
     * @return int
     */
    @Override
    public int customerCount(CustomerQueryParam customerQueryParam) {
        PageInfo pageInfo = customerQueryParam.getPageInfo();
        Node customerCount = customerDao.customerCount(customerQueryParam.getCustSname(), customerQueryParam.getCustId(), pageInfo.getPageSize(), pageInfo.getPageOffSet());
        return customerCount.valueInt();
    }

    /**
     * 添加客户
     *
     * @param customer 客户
     */
    @Override
    public void addCustomer(Customer customer) {
        customer.setUpdateTm(LocalDateTime.now());
        customerDao.addCustomer(customer);
    }


    /**
     * 更新客户
     *
     * @param customer 客户
     */
    @Override
    public void updateCustomer(Customer customer) {
        customer.setUpdateTm(LocalDateTime.now());
        customerDao.updateCustomer(customer);
    }

    /**
     * 得到客户
     *
     * @param customerId 客户id
     * @return {@link Customer}
     */
    @Override
    public Customer getCustomer(String customerId) {
        MapNode customer = customerDao.getCustomer(customerId);
        return JSON.parseObject(JSON.toJSONString(customer.toMap()), Customer.class);
    }


    /**
     * 选择客户列表
     *
     * @param customerQueryParam 客户查询参数
     * @return {@link List<Customer>}
     */
    @Override
    public List<Customer> selectCustomerList(CustomerQueryParam customerQueryParam) {
        //PageInfo pageInfo = customerQueryParam.getPageInfo();
        // 前端暂时要求查所有
        TableNode tableNode = customerDao.selectCustomerList(customerQueryParam.getCustSname(), customerQueryParam.getCustId(), null, null);
        return JSON.parseArray(tableNode.toJson(), Customer.class);
    }



}
